package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import anet.channel.util.f;

import com.itboye.textileexpes.beans.ColletionFenyeBeans;
import com.itboye.textileexpes.beans.GoodsBean;
import com.itboye.textileexpes.beans.GoodsSearch;
import com.itboye.textileexpes.beans.GoodsTopBean;
import com.itboye.textileexpes.presenter.ColloctionPresenter;
import com.itboye.textileexpes.presenter.GoodsPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexpres.widget.EditTextWithDelete;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActivityTypeDetailTwo extends BaseActivity implements Observer,
		TextWatcher {
	ListView list;
	MyTypeDetailAdapter adapter;
	ImageView back;
	TextView title;
	GoodsPresenter goodsPresenter;
	EditTextWithDelete key_word;
	TextView search;
	GoodsBean bean;
	GoodsBean goodsBean = new GoodsBean();
	List<GoodsBean.GoodsList> goodsBeanslist = new ArrayList<GoodsBean.GoodsList>();
	RelativeLayout null_search;
	String cateID = "";
	String PropID = "";
	String from = "";
	String name = "";
	ColloctionPresenter colloctionPresenter;
	String pid;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_type_detail;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		showProgressDialog("正在加载", true);
		goodsPresenter = new GoodsPresenter(this);
		key_word.setFocusable(true);
		key_word.setFocusableInTouchMode(true);
		key_word.requestFocus();
		bean = new GoodsBean();
		cateID = getIntent().getStringExtra("cateid");
		PropID = getIntent().getStringExtra("proid");
		from = getIntent().getStringExtra("from");
		name = getIntent().getStringExtra("name");
		bean.setUid(IsMobileNumPhone.isUid());
		if ("has_prop".equals(from)) {
			bean.setPage_index("1");
			bean.setPage_size("10");
			bean.setCate_id(cateID);
			bean.setProp_id(PropID);
			goodsPresenter.getInfo(bean);
		} else if ("keyword".equals(from)) {
			bean.setKeyword("" + name);
			goodsPresenter.getInfo(bean);
		}
		colloctionPresenter = new ColloctionPresenter(this);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}

		});
		key_word.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bean = new GoodsBean();
				key_word.setFocusable(true);
				key_word.setFocusableInTouchMode(true);
				if (from.equals("keyword")) {
					bean.setUid(IsMobileNumPhone.isUid());
					bean.setKeyword("");
					goodsPresenter.getInfo(bean);
				}
				if (from.equals("has_prop")) {
					bean.setUid(IsMobileNumPhone.isUid());
					bean.setPage_index("1");
					bean.setPage_size("10");
					bean.setUid(IsMobileNumPhone.isUid());
					goodsPresenter.getInfo(bean);
				}

			}
		});
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityTypeDetailTwo.this,
						ActivityTypeDetailTwo.class);
				intent.putExtra("name", key_word.getText() + "");
				intent.putExtra("from", "keyword");
				startActivity(intent);
				finish();
			}
		});
	}

	class MyTypeDetailAdapter extends BaseAdapter {
		Context context;
		List<GoodsBean.GoodsList> goodsBeanslist;

		// List<GoodsSearch> goodsBeanslist;

		public MyTypeDetailAdapter(Context context,
				List<GoodsBean.GoodsList> goodsBeanslist) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.goodsBeanslist = goodsBeanslist;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return goodsBeanslist == null ? 0 : goodsBeanslist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewHolder holder = null;
			if (convertView == null) {
				holder = new viewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_goods_1, null);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
			holder.title.setText(goodsBeanslist.get(position).getName());
			holder.number.setText(goodsBeanslist.get(position)
					.getProduct_code());
			holder.context.setText(goodsBeanslist.get(position)
					.getSecondary_headlines());
			holder.publish_time.setText(TimeFormat.DateFormat(goodsBeanslist
					.get(position).getCreatetime()));
			if ("0".equals(goodsBeanslist.get(position).getIs_fav())) {
				holder.collection_img
						.setImageResource(R.drawable.collect_collection);
			} else {
				holder.collection_img
						.setImageResource(R.drawable.home_collection_img);
			}
			XImageLoader.load(
					GlobalConfig.IMAGE_DOUBLE
							+ goodsBeanslist.get(position).get_imgs().get(0),
					holder.goods_img);
			holder.collection_img.setTag(position);
			holder.collection_img.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (IsMobileNumPhone.isUid() == null
							|| "".equals(IsMobileNumPhone.isUid())) {
						toast("还未登录");
						return;
					} else {
						ColletionFenyeBeans model = new ColletionFenyeBeans();
						model.setUid(IsMobileNumPhone.isUid());
						model.setFType("1");
						model.setIds(goodsBeanslist.get((Integer) v.getTag())
								.getId());
						pid = goodsBeanslist.get((Integer) v.getTag()).getId();
						colloctionPresenter.add(model);
					}
				}
			});
			return convertView;
		}

	}

	class viewHolder {
		TextView title, context, publish_time, number;
		ImageView goods_img, collection_img, delete_img;
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GoodsPresenter.Fenye_success) {
				goodsBeanslist.clear();
				goodsBean = (GoodsBean) entity.getData();
				goodsBeanslist.addAll(goodsBean.getList());
				if (goodsBean == null || (goodsBean.getList().size() == 0)) {
					null_search.setVisibility(View.VISIBLE);
					list.setVisibility(View.GONE);
				} else {
					null_search.setVisibility(View.GONE);
					list.setVisibility(View.VISIBLE);
				}
				adapter = new MyTypeDetailAdapter(ActivityTypeDetailTwo.this,
						goodsBeanslist);
				list.setAdapter(adapter);

				list.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(ActivityTypeDetailTwo.this,
								GoodsDetail.class);
						intent.putExtra("pid", goodsBeanslist.get(arg2).getId());
						startActivity(intent);
					}
				});

			}
			if (entity.getEventTag() == ColloctionPresenter.Add_success) {
				Intent intent = new Intent("UpDateCollection");
				sendBroadcast(intent);
				Iterator<GoodsBean.GoodsList> iterator = goodsBeanslist
						.iterator();
				while (iterator.hasNext()) {
					GoodsBean.GoodsList getAList = iterator.next();
					if (getAList.getId().equals(pid)) {
						if (!"0".equals(getAList.getIs_fav())) {
							getAList.setIs_fav("0");
						} else {
							getAList.setIs_fav("1");
						}
						break;
					}
				}
				adapter.notifyDataSetChanged();
			}
			try {
				closeProgressDialog();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}
}
